package GUI;

import 学生.Student;
import 数据库和网络.myDatebase;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

public class chooseRankStudentWindow extends JFrame {
    JPanel mainPanel;
    ArrayList<Student> stuList;
    static chooseRankStudentWindow chooseRank;

    public chooseRankStudentWindow(){

        myDatebase classconsulter=new myDatebase("teacher");
        myDatebase infoClass=new myDatebase("information_schema");
        myDatebase stusel=new myDatebase("student");
        ArrayList<String> classList=new ArrayList<>();
        ArrayList<String> realclasslist =new ArrayList<>();
        stuList=new ArrayList<>();
        String sql="select * from teacher.teacher where teacherID='"+enterWindow.currentTeacher.getSchoolID()+"'";
        try {
            classconsulter.statement.execute(sql);
            classconsulter.resultSet=classconsulter.statement.executeQuery(sql);

            sql ="select * from INNODB_TABLES where NAME like '%papercollection%%class%'";
            infoClass.statement.execute(sql);
            infoClass.resultSet=infoClass.statement.executeQuery(sql);
            ArrayList<String> NamList=new ArrayList<>();
            while (infoClass.resultSet.next()){
                String linshi=infoClass.resultSet.getString(2).substring(16);
                classList.add(linshi);
                System.out.println(linshi);
            }
            sql="select * from teacher.teacher where teacherID="+enterWindow.currentTeacher.getSchoolID()+"";
            classconsulter.statement.execute(sql);
            classconsulter.resultSet=classconsulter.statement.executeQuery(sql);
            while(classconsulter.resultSet.next()){
                Iterator<String> ite=classList.iterator();
                while(ite.hasNext()){
                    String j=ite.next();
                    if(classconsulter.resultSet.getString(j).equals("true")){
                        realclasslist.add(j);
                        System.out.println(j);
                    }
                }
            }

            Iterator<String> clss=realclasslist.iterator();

            while(clss.hasNext()){
                String cllaass=clss.next();
                sql="select * from student.student where student.student.class='"+cllaass+"'";
                stusel.statement.execute(sql);
                stusel.resultSet=stusel.statement.executeQuery(sql);
                Student tempStu;
                while(stusel.resultSet.next()){
                    tempStu=new Student(stusel.resultSet.getString("StudentName")
                            ,stusel.resultSet.getString("StudentID")
                            ,stusel.resultSet.getString("class"));
                    stuList.add(tempStu);
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        mainPanel = new JPanel();
        JLabel lab1=new JLabel("学生列表：");
        Font bigFont = new Font("宋体",Font.BOLD,24);
        lab1.setFont(bigFont);
        JButton but = new JButton("返回");
        this.setSize(800,600);
        mainPanel.setSize(this.getSize());
        mainPanel.setLayout(null);
        lab1.setBounds(50,30,300,50);



        int size= stuList.size();
        for (int i=0 ; i<size;i++){
            chooseRankStudentItem.chooseRankStudentItem = new chooseRankStudentItem(i+1,stuList.get(i));
            mainPanel.add(chooseRankStudentItem.chooseRankStudentItem.reviseStudent);
        }


        mainPanel.add(lab1);

        mainPanel.add(but);
        but.setBackground(Color.lightGray);
        but.addActionListener(new backListener());
        this.setContentPane(mainPanel);
        this.setLocationRelativeTo(null);//居中
        this.setVisible(true);
        this.setTitle("在线考试平台");

        //this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    public class backListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {//关闭注册界面
            chooseRankStudentWindow.this.dispose();

        }
    }





}
